﻿using UnityEngine;
using System.Collections;

public class PlayerCursor : MonoBehaviour {

	public GameObject source;
	public GameObject spot;
	
	public Vector3 last_mous_pos;
	
	
	
	void Beam(Vector3 p)
	{
		Vector3 dir = p-source.transform.position;
		
		RaycastHit2D hit = Physics2D.Raycast(source.transform.position,dir.normalized,100f);
		
		if(hit.point.y != 0f)
			spot.transform.position = new Vector3(hit.point.x,hit.point.y,1f) ;
	}
	
	// Update is called once per frame
	void Update () {
	
		if(source == null || spot == null)
			return;
		
		
		if(Input.mousePosition != last_mous_pos)
		{
			Vector3 p = Camera.main.ScreenPointToRay(Input.mousePosition).GetPoint(-Camera.main.transform.position.z);
			p.z=transform.position.z;
			transform.position = p;
			
			transform.GetChild(0).renderer.enabled = true;
			
			///
			if(spot!= null && source!=null);
				Beam(p);
		}
		else
		{
			if(spot!= null && source!=null);
				Beam(transform.position);
		}
		
		last_mous_pos = Input.mousePosition;
	}
}
